﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MoneyLover.WCFMoneyLover;

namespace MoneyLover.Pages
{
    public partial class KetQuaTimKiem : PhoneApplicationPage
    {
        public KetQuaTimKiem()
        {
            InitializeComponent();
        }
        MoneyLoverServiceClient _db = new MoneyLoverServiceClient();
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            Loaddatabasefind();
        }

        private void Loaddatabasefind()
        {
            String keyword = NavigationContext.QueryString["keyword"];
            String tien1 = NavigationContext.QueryString["tien1"];
            String tien2 = NavigationContext.QueryString["tien2"];
            String ngay1 = NavigationContext.QueryString["ngay1"];
            String ngay2=NavigationContext.QueryString["ngay2"];
            decimal _temp = decimal.Parse("-1");
            String giaodich = NavigationContext.QueryString["Giaodich"];
            decimal tiendau=-1;
            decimal tiencuoi = -1;
            if (!String.IsNullOrEmpty(tien1))
            {
                 tiendau=decimal.Parse(tien1);
            }

            if (!String.IsNullOrEmpty(tien2))
           {
               tiencuoi=decimal.Parse(tien2);
           }
            int _giaodich=int.Parse(giaodich);
            DateTime ngayA = new  DateTime();
            if (!String.IsNullOrEmpty(ngay1))
            {
                ngayA = Converttodate(ngay1);
            }
            DateTime NgayB = new DateTime();
            if (!String.IsNullOrEmpty(ngay2))
            {
                NgayB = Converttodate(ngay2);
            }
            if (_giaodich.Equals(1))
            {
                lstthuchi.Visibility = Visibility.Visible;
                lstvayno.Visibility = Visibility.Collapsed;
                _db.TimKiemThuCompleted += _db_TimKiemThuCompleted;
                _db.TimKiemThuAsync(keyword,tiendau,tiencuoi,ngayA,NgayB);
            }else if (_giaodich.Equals(2))
            {
                lstthuchi.Visibility = Visibility.Visible;
                lstvayno.Visibility = Visibility.Collapsed;
                _db.TimKiemChiCompleted += _db_TimKiemChiCompleted;
                _db.TimKiemChiAsync(keyword, tiendau, tiencuoi, ngayA, NgayB);
            }
            else if (_giaodich.Equals(3))
            {
                lstthuchi.Visibility = Visibility.Collapsed;
                lstvayno.Visibility = Visibility.Visible;
                _db.TimKiemVayCompleted += _db_TimKiemVayCompleted;
                _db.TimKiemVayAsync(keyword, tiendau, tiencuoi, ngayA, NgayB);
          
            }
            else
            {
                lstthuchi.Visibility = Visibility.Collapsed;
                lstvayno.Visibility = Visibility.Visible;
                _db.TimKiemNoCompleted += _db_TimKiemNoCompleted;
                _db.TimKiemNoAsync(keyword, tiendau, tiencuoi, ngayA, NgayB);
            }
           
        }
        void _db_TimKiemNoCompleted(object sender, TimKiemNoCompletedEventArgs e)
        {
             if (e.Result != null)
            {
                lstthuchi.ItemsSource = e.Result;
            }
        }
        void _db_TimKiemVayCompleted(object sender, TimKiemVayCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                lstthuchi.ItemsSource = e.Result;
            }
        }
        void _db_TimKiemChiCompleted(object sender, TimKiemChiCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                lstthuchi.ItemsSource = e.Result;
            }
        }
        void _db_TimKiemThuCompleted(object sender, TimKiemThuCompletedEventArgs e)
        {
           if (e.Result!=null)
           {
               lstthuchi.ItemsSource = e.Result;
           }
        }
        private DateTime Converttodate(String chuoi)
        {
                DateTime temp;
                string[] split = chuoi.Split(new Char[] { '/' });
                int ngay = int.Parse(split[0]);
                int Thang = int.Parse(split[1]);
                int nam = int.Parse(split[2]);
                temp = new DateTime(nam, Thang, ngay);
                return temp;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}